local SkillCallObj = class("SkillCallObj")
local AttributeConst = require("const.AttributeConst")

function SkillCallObj:ctor(entity, callId)
    self._entity = entity
    self._callId = callId
end

function SkillCallObj:call(report)
    local cfg = require("config.call_obj").get(self._callId)
    assert(cfg, "can not found cfg in call_obj by id=" .. self._callId)
    local callFunc = self["_" .. cfg.type]
    if callFunc then
        callFunc(self, cfg, report)
    end
end

function SkillCallObj:_initCallObjAttrs(cfg, entity)
    if cfg.attr > 0 then
        local attrId = cfg.attr
        if AttributeConst.INIT_MAP[attrId] then
            attrId = AttributeConst.INIT_MAP[attrId]
        end
        local attrValue = self._entity:getAttrValue(attrId)
        local attrs = {
            [cfg.attr] = attrValue * (cfg.per / 1000)
        }
        entity:initFightAttr(attrs)
    else
        local attrs = self._entity:getAttrs()
        local result = {}
        for id, value in pairs(attrs) do
            result[id] = value * (cfg.per / 1000)
        end
        result[AttributeConst.INIT_BLOOD] = nil
        result[AttributeConst.INIT_MANA] = nil
        entity:initFightAttr(result)
    end
end

function SkillCallObj:_fillingEntity(entity, cfg, report)
    local camp = self._entity:getCamp()
    local excuter = self._entity:getExcuter()
    local element = self._entity:getElement()
    entity:setContinuance(cfg.continuce)
    entity:setCamp(camp)
    -- entity:setElement(element)
    local stand = excuter:getUnusedStand(cfg.pos, camp)
    if stand.use == true then
        local entity = excuter:getCallEntityByStandCamp(stand, camp)
        entity:setDie(true)
        report:addEntityDie(entity)
    end
    entity:setStanding(stand)
    self:_initCallObjAttrs(cfg, entity)
    excuter:addCallEntity(entity)
    report:addCallObj(entity, self._callId)
end

function SkillCallObj:_HATRED(cfg, report)
    local StandingEntity = require("gameLogic.fight.fightEntity.StandingEntity")
    local excuter = self._entity:getExcuter()
    local fightEntityId = excuter:getFightEntityId()
    local entity = StandingEntity.new(excuter, fightEntityId, self._entity:getElement())
    self:_fillingEntity(entity, cfg, report)
end

function SkillCallObj:_COLLABORATION(cfg, report)
    local CollaborationEntity = require("gameLogic.fight.fightEntity.CollaborationEntity")
    local excuter = self._entity:getExcuter()
    local fightEntityId = excuter:getFightEntityId()
    local skillCfg = require("config.fight_skill").get(cfg.skillId)
    local entity = CollaborationEntity.new(excuter, skillCfg, fightEntityId, self._entity:getElement())
    local behav = require("gameLogic.fight.fightBehavior.SelectLastBehavior").new()
    entity:setBehavior(behav)
    self:_fillingEntity(entity, cfg, report)
end

function SkillCallObj:_INHERIT(cfg, report)
    local CallEntity = require("gameLogic.fight.fightEntity.CallEntity")
    local excuter = self._entity:getExcuter()
    local fightEntityId = excuter:getFightEntityId()
    local skillCfg = self._entity:getNormalSKill():getConfig()
    local entity = CallEntity.new(excuter, skillCfg, fightEntityId, self._entity:getElement())
    local behav = require("gameLogic.fight.fightBehavior.AutoBehavior").new()
    entity:setBehavior(behav)
    self:_fillingEntity(entity, cfg, report)
end

return SkillCallObj
